package artibella.ui.combos
{
	import artibella.ui.scrolls.AdaptableScroller;
	import artibella.ui.scrolls.ScrollBox;
	import artibella.ui.scrolls.ScrollBar;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class ComboList extends Sprite
	{
		private var _combo:ACombo;
		
		private var _tabItems:Array;
		
		private var _list:Sprite;
		private var _bg:Sprite;
		
		private var _box:ScrollBox;
		private var _scrollbar:ScrollBar;
		
		public function ComboList( c:ACombo ) 
		{
			_combo = c;
			
			_init();
		}
		
		private function _init():void
		{
			_tabItems = new Array();
			
			_bg = new Sprite;
			addChild( _bg );
			
			_list = new Sprite();
			addChild( _list );
		}
		
		public function addItem( c:AComboListItem ):void
		{
			c.x = 0;
			c.y = 2 + c.height * _tabItems.length;
			_list.addChild( c );
			
			_tabItems.push( c );
		}
		
		public function finalize( w:int, h:int ):void
		{
			
			_bg.graphics.beginFill( 0xFFFFFF );
			_bg.graphics.drawRect( 0, 0, w, h );
			_bg.graphics.endFill();
			
			_bg.graphics.lineStyle( 1 , 0x000000, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE, JointStyle.MITER, 3  );
			_bg.graphics.moveTo( 1, 0 );
			_bg.graphics.lineTo( 1, h - 1 );
			_bg.graphics.lineTo( w - 1, h - 1 );
			_bg.graphics.lineTo( w - 1, 0 );
			
			_setScroller( w, h );
			
			_list.mouseChildren = true;
			_list.addEventListener( MouseEvent.ROLL_OVER, _doROver, true );
			_list.addEventListener( MouseEvent.ROLL_OUT, _doROut, true );
			_list.addEventListener( MouseEvent.CLICK, _doSelect, true );
		}
		
		private function _doROver( e:MouseEvent ):void
		{
			var item:AComboListItem = e.target as AComboListItem;
			item.doROver();
		}
		
		private function _doROut( e:MouseEvent ):void
		{
			var item:AComboListItem = e.target as AComboListItem;
			item.doROut();
		}
		
		
		private function _doSelect( e:MouseEvent ):void
		{
			trace( "SELECT" );
			
			_combo.selectItem( e );
		}
		
		// - - -
		
		private function _setScroller( w:int, h:int ):void
		{
			_box = new ScrollBox( w - 20, h );
			
			_scrollbar = new ScrollBar( _box, false );
			
			var bar:Sprite = new Sprite();
			bar.graphics.lineStyle( 1, 0xC6C4C7 );
			bar.graphics.lineTo( 0, h-1 );
			bar.graphics.endFill();
			bar.graphics.lineStyle();
			bar.graphics.beginFill( 0xFFFFFF, 0 );
			bar.graphics.drawRect( 0, 2, 20, h-2 );
			bar.graphics.endFill();
			_scrollbar.setBar( bar );

			var scroller:AdaptableScroller = new AdaptableScroller();
			scroller.graphics.beginFill( 0xFFFFFF, 0 );
			scroller.graphics.drawRect( 0, 0, 20, 14 );
			scroller.graphics.beginFill( 0x000000, .5 );
			scroller.graphics.drawRect( 4, 4, 12, 6 );
			scroller.graphics.endFill();
			scroller.scale9Grid = new Rectangle( 5, 5, 10, 5 );
			_scrollbar.setScroller( scroller, true );
			
			_box.setScrollingTool( _scrollbar );
			
			_box.setContent( _list );
			
			_box.x = 0;
			_box.y = 0;
			addChild( _box );
			
			_scrollbar.x = w - 20;
			_scrollbar.y = 0;
			addChild( _scrollbar );
		}

		// - - -
		
		public function get nbItems():int
		{
			return _tabItems.length;
		}
		
	}

}